package com.gemptc.yueban.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.gemptc.yueban.daoImp.InsertActivityImp;

/**
 * Servlet implementation class YueBan_FaQi
 */
@WebServlet("/YueBan_FaQi")
public class YueBan_FaQi extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public YueBan_FaQi() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		Integer user_id = null,/* ac_contact = null,*/  ac_number = null;
		String ac_pic_path, ac_contact = null, ac_title = null, ac_time = null, ac_lastTime = null, ac_venue = null, ac_venue_longitude = null, ac_venue_latitude = null, ac_destination = null, ac_destination_longitude = null, ac_destination_latitude = null, ac_content = null;
		long date = (new java.util.Date()).getTime();
		ac_pic_path = String.valueOf(date + ".jpg");
		//String  user_id=request.getParameter("user_id");
		DiskFileUpload fu = new DiskFileUpload();
		List fileItems = null;
		try {
			fileItems = fu.parseRequest(request);
			// 依次处理每个上传的文件
			Iterator iter = fileItems.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				String filedname = item.getFieldName();
				// System.out.println("jiaojiao" + item.getString());
				if (item.isFormField()) {
					if (filedname.equals("user_id")) {
						user_id = Integer.parseInt(item.getString());
					} else if (filedname.equals("ac_title")) {
						ac_title = item.getString();
					} else if (filedname.equals("ac_time")) {
						ac_time = item.getString();
					} else if (filedname.equals("ac_lastTime")) {
						ac_lastTime = item.getString();
					} else if (filedname.equals("ac_venue")) {
						ac_venue = item.getString();
					} else if (filedname.equals("ac_venue_longitude")) {
						ac_venue_longitude = item.getString();
					} else if (filedname.equals("ac_venue_latitude")) {
						ac_venue_latitude = item.getString();
					} else if (filedname.equals("ac_destination")) {
						ac_destination = item.getString();
					} else if (filedname.equals("ac_destination_longitude")) {
						ac_destination_longitude = item.getString();
					} else if (filedname.equals("ac_destination_latitude")) {
						ac_destination_latitude = item.getString();
					} else if (filedname.equals("ac_num")) {
						ac_number = Integer.parseInt(item.getString());
					} else if (filedname.equals("ac_contact")) {
						ac_contact = item.getString();
					} else if (filedname.equals("ac_content")) {
						ac_content = item.getString();
					}
				} else {
					item.write(new java.io.File(
							"D:\\workspace\\Jinnang7\\WebContent\\img\\"
							/*this.getServletContext().getRealPath("/")
							+ "img\\"*/
									+ ac_pic_path));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//String ac_date = String.valueOf(date);
		Date currentTime = new Date();
		String ac_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(currentTime);
		
		InsertActivityImp insertActivityImp = new InsertActivityImp();
		System.out.println("传的值"+user_id+"user-id"+ ac_contact+"ac_contact"+ac_number+"ac_number"+
				ac_title+"ac_title"+ac_time+"ac_time"+ac_lastTime+"ac_lastTime"+ac_venue+"ac_venue"+ ac_venue_longitude+"ac_venue_longitude"+
				ac_venue_latitude+"ac_venue_latitude"+ac_destination+"ac_destination"+ ac_destination_longitude+"ac_destination_longitude"+
				ac_destination_latitude+"ac_destination_latitude"+ac_content+"ac_content"+ ac_pic_path+"ac_pic_path"+ ac_date+"ac_date");
		insertActivityImp.InsertActivity(user_id, ac_contact, ac_number,
				ac_title, ac_time, ac_lastTime, ac_venue, ac_venue_longitude,
				ac_venue_latitude, ac_destination, ac_destination_longitude,
				ac_destination_latitude, ac_content, ac_pic_path, ac_date);
	}

}
